package com.service.impl;

import com.dao.UserDao;
import com.pojo.SysUser;
import com.service.ResetPasswordService;
import com.util.BCryptPasswordEncoderUtils;
import com.util.ConstUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/19 - 17:37
 */
@Service
@Transactional
public class ResetPasswordServiceImpl implements ResetPasswordService {
    @Autowired
    private UserDao userDao;
    public Map<String,Object> resetPassword(String oldPwd, String newPwd, Principal principal) {
        SysUser loginUser = userDao.findUserInfoByUserName(principal.getName());
        Map<String,Object> maps = new HashMap<String, Object>();
        if(BCryptPasswordEncoderUtils.decode(oldPwd,loginUser.getPassword())){
            //正确
            maps.put(ConstUtils.MATCHE,true);
            SysUser sysUser = new SysUser();
            sysUser.setId(loginUser.getId());
            sysUser.setPassword(BCryptPasswordEncoderUtils.encode(newPwd));
            if(userDao.updateUser(sysUser) > 0){
                maps.put(ConstUtils.SUCCESS,true);
                maps.put(ConstUtils.MESSAGE,"修改成功，即将退出登录...");
            }else{
                maps.put(ConstUtils.SUCCESS,false);
                maps.put(ConstUtils.MESSAGE,"修改失败，请重试！");
            }
        }else{
            maps.put(ConstUtils.EXIST,false);
            maps.put(ConstUtils.MESSAGE,"你的原密码不正确，请重新输入！");
        }
        return maps;
    }
}
